import FetchHelper from '../helper/fetch-helper';
import { isIntersects, wktToGeom } from "../helper/mapHelper";
import _ from 'lodash';

export default class ZhfxApi {
    static async listByPolygon(polygon) {
        // const resp = await FetchHelper.postJson(`/api/yuqing_datafindby`, params)
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/yhsj/list`)
        const datas = resp.rows?.filter(item => {
            if (!item.geom) {
                return false;
            }
            const geom = wktToGeom(item.geom);
            return isIntersects(geom, polygon)
        })
        const groupData = _.groupBy(datas || [], 'zhfxxl');
        return {
            data: _.keys(groupData).map(key => {
                return {
                    name: key,
                    count: groupData[key].length,
                    list: groupData[key]
                }
            })
        };
    }
    static async list(params) {
        try {
            const resp = await FetchHelper.getJson(`/admin-api/ybyj/yhsj/list`, {
                //xzqhbm: window.userAreaCode,
                ...params
            })
            resp.rows = resp?.rows?.map(item => {
                if (item?.xq) {
                    try {
                        const json = JSON.parse(item?.xq);
                        return {
                            ...json,
                            ...item,
                        }
                    } catch (e) {

                    }

                }
                return item;
            })
            return resp;

        } catch (err) {
            return { rows: [] }
        }
    }
}
